//页面初始化
$(function(){
	// 初始化tab
	initTab();
//	//关闭新增窗口
	$("#closeBtn").click(function() {
		backendCommon.closeWin("noPassWin");
	});
});


/**
 * 查询列表
 */
function findInfoPublishPg(pushStatis){
	var rn = Math.random();//删除缓存
	$('#tt'+pushStatis).datagrid('options').url=  mainDomain+'/info/findInfoPushPgByDto.action';
    $('#tt'+pushStatis).datagrid('options').queryParams = {
			'pushStatis':pushStatis,
			'rn':rn
	};
	$('#tt'+pushStatis).datagrid('reload');
}

//打开审核拒绝窗口
function openNoPassWin(id) {
	//初始化窗口
	$("#appId").val(id);
	backendCommon.formReset("noPassForm");
	backendCommon.openeWin('noPassWin');
	$("#saveBtn").unbind('click').click(function() {
		verifyNo();
	});
}

// 审核通过
function verifyPass(id){
	var param = {"id":id,'pushStatis':1};
	$.post(mainDomain+'/info/infoPushVerifyPass.action', param, function(result){
		if (result.success && result.state == 1) {
			$.messager.alert('提示','审核推送成功', 'info');
			var options = $('#tt0').datagrid('getPager').data("pagination").options;  
            var curr = options.pageNumber;  
            $('#tt0').datagrid('getPager').pagination({
                 'pageNumber' : curr
            });
            $('#tt0').datagrid('reload');
		} else {
			$.messager.alert('提示', result.message, 'error');
		}
	});
}

// 审核拒绝
function verifyNo(){
	var id = $('#appId').val();
	var remark = $('#remark').val().trim();
	if(remark == ''){
		$.messager.alert('提示', '回复意见不能为空', 'error');
		return false;
	}
	if(remark.length > 250){
		$.messager.alert('提示', '回复意见不能操作250个长度', 'error');
		return false;
	}
	var param = {
			'id':id,
			'remark':remark,
			'pushStatis':2
	};
	$.post(mainDomain+'/info/infoPushVerifyNoPass.action', param, function(result){
		if (result.success && result.state == 1) {
			$.messager.alert('提示','操作成功', 'info');
			backendCommon.closeWin('noPassWin');
			var options = $('#tt0').datagrid('getPager').data("pagination").options;  
            var curr = options.pageNumber;  
            $('#tt0').datagrid('getPager').pagination({
                 'pageNumber' : curr
            });
            $('#tt0').datagrid('reload');
		} else {
			$.messager.alert('提示', result.message, 'error');
		}
	});
}

// 进入发布申请 详情页 
function intoPublishDetailPage(id){
	window.open(mainDomain+'/info/infoPushDetailPage.action?id='+id,"推送详情","height=600,width=920,top=50,left=100,toolbar=no,titlebar=yes,resizable=no,menubar=no,scrollbars=yes, resizable=no,location=no, status=no");
}

// 初始化tab
var initTab = function(){
	$('#tab1').tabs('add',
    	{    
	    title:'待审核',    
	    content:"<table id='tt0' style='padding:20px;margin-top:10px;'></table>",    
	    closable:false,
	    tools:[{
			iconCls:'icon-reload',
			handler:function(){
				initTable0();
			}
		}]
	});
    
    $('#tab1').tabs('add',
    	{    
	    title:'审核通过',    
	    content:"<table id='tt1' style='padding:10px;'></table>",    
	    closable:false,
	    tools:[{
			iconCls:'icon-reload',
			handler:function(){
				initTable1();
			}
		}]
	});
    
    $('#tab1').tabs('add',
    	{    
	    title:'审核未通过',    
	    content:"<table id='tt2' style='padding:10px;'></table>",    
	    closable:false,
	    tools:[{
			iconCls:'icon-reload',
			handler:function(){
				initTable2();
			}
		}]
    });
    
    $('#tab1').tabs('add',
        	{    
    	    title:'推送失败',    
    	    content:"<table id='tt3' style='padding:10px;'></table>",    
    	    closable:false,
    	    tools:[{
    			iconCls:'icon-reload',
    			handler:function(){
    				initTable3();
    			}
    		}]
        });
    $('#tab1').tabs({
        border:false,
        fit:true,
        tabHeight:36,
        onSelect:function(title,index){
    		if(title == '待审核'){
    			initTable0();
    		}
    		if(title == '审核通过'){
    			initTable1();
    		}
    		
    		if(title == '审核未通过'){
    			initTable2();
    		}
    		
    		if(title == '推送失败'){
    			initTable3();
    		}
        }
    });
};

//初始化列表页面
function initTable0() {
	$('#tt0').datagrid({
		title:'待审核列表信息',
		width : backendCommon.getWidth(0.94),
		height : backendCommon.getHeight(0.80),
		method:'post',
		loadMsg:"数据装载中,请稍等....",
		url:mainDomain+'/info/findInfoPushPgByDto.action?pushStatis=0',
		rownumbers:true,
		nowrap: false,
		fitColumns: true,
		pagination:true,
        singleSelect:true,
        pageSize:10,
	    pageNumber:1,          
	    pageList:[10,15,20],
		columns : [[
					{field : 'uid',title : '操作',width : $(this).width() * 0.15,align : 'center',
						 formatter:function(value,rec){
							 var b = '<a class="easyui-linkbutton" data-options="iconCls:\'icon-ok\'"  onclick="verifyPass('+rec.id+');" href="javascript:void(0)"  style="width:80px" >审核通过</a>';
							 b = b +'&nbsp;';
							 b = b+'<a class="easyui-linkbutton" data-options="iconCls:\'icon-no\'"  onclick="openNoPassWin('+rec.id+');" href="javascript:void(0)"  style="width:80px" >审核拒绝</a>';
							 return b;
						   }
					},
					{field : 'createName',title : '提交人',width :$(this).width() * 0.10,align : 'center'},
					{field : 'content',title : '内容',width : $(this).width() * 0.35,align : 'center',
						formatter:function(value,rec){
							var count = '<a onclick="intoPublishDetailPage('+rec.id+');" href="javascript:void(0)">';
							if(rec.detailVoList != null && rec.detailVoList.length>0){
								$.each(rec.detailVoList,function(index,detail){
									count +=detail.infoTitle+'<br/>';
								});
							}
							count +='</a>';
							return count;
						 }
					},
					{field : 'pushType',title : '推送方式',width : $(this).width() * 0.35,align : 'center',
						formatter:function(value,rec){
							if(value==1){
								return '立即推送';
							}else if(value == 2){
								return '预约推送<br>推送时间['+backendCommon.dateTimeToLocalStringWithSeconds(rec.appointmentTime)+']';
							}
						 }
					},
					{field : 'createTime',title : '提交时间',width :$(this).width() * 0.20,align : 'center',
						formatter : function(value, rec, index) {
							var str = "";
							if(value != '') {
								str = backendCommon.dateTimeToLocalStringWithSeconds(rec.createTime);
							}
							return str;
						}
					},
					{field : 'updateTime',title : '操作时间',width :$(this).width() * 0.20,align : 'center',
						formatter : function(value, rec, index) {
							var str = "";
							if(value != '') {
								str = backendCommon.dateTimeToLocalStringWithSeconds(value);
							}
							return str;
						}
					}
				]]
	});
};

function initTable1(){
	$('#tt1').datagrid({
		title:'审核通过列表信息',
		width : backendCommon.getWidth(0.94),
		height : backendCommon.getHeight(0.80),
		method:'post',
		loadMsg:"数据装载中,请稍等....",
		url:mainDomain+'/info/findInfoPushPgByDto.action?pushStatis=1',
		rownumbers:true,
		nowrap: false,
		fitColumns: true,
		pagination:true,
        singleSelect:true,
        pageSize:10,
	    pageNumber:1,          
	    pageList:[10,15,20],
		columns : [[
					{field : 'createName',title : '提交人',width : $(this).width() * 0.10,align : 'center'},
					{field : 'content',title : '内容',width : $(this).width() * 0.35,align : 'center',
						formatter:function(value,rec){
							var count = '<a onclick="intoPublishDetailPage('+rec.id+');" href="javascript:void(0)">';
							if(rec.detailVoList != null && rec.detailVoList.length>0){
								$.each(rec.detailVoList,function(index,detail){
									count +=detail.infoTitle+'<br/>';
								});
							}
							count +='</a>';
							return count;
						 }
					},
					{field : 'pushStatis',title : '状态',width : $(this).width() * 0.20,align : 'center',
						formatter : function(value, rec, index) {
							if(value == 1) {
								return '<span style="color:red;">审核待推送</span><br>推送时间['+backendCommon.dateTimeToLocalStringWithSeconds(rec.appointmentTime)+']';
							}else if(value == 3){
								return '<span style="color:green;">审核已推送</span>';
							} 
							return '';
						}
					},
					{field : 'applierName',title : '审核人',width : $(this).width() * 0.10,align : 'center'},
					{field : 'applyTime',title : '审核时间',width : $(this).width() * 0.10,align : 'center',
						formatter : function(value, rec, index) {
							var str = "";
							if(value != '') {
								str = backendCommon.dateTimeToLocalStringWithSeconds(value);
							}
							return str;
						}
					},
					{field : 'updateTime',title : '操作时间',width : $(this).width() * 0.10,align : 'center',
						formatter : function(value, rec, index) {
							var str = "";
							if(value != '') {
								str = backendCommon.dateTimeToLocalStringWithSeconds(value);
							}
							return str;
						}
					}
				]]
	});
}
function initTable2(){
	$('#tt2').datagrid({
		title:'审核未通过列表信息',
		width : backendCommon.getWidth(0.94),
		height : backendCommon.getHeight(0.80),
		method:'post',
		loadMsg:"数据装载中,请稍等....",
		url:mainDomain+'/info/findInfoPushPgByDto.action?pushStatis=2',
		rownumbers:true,
		nowrap: false,
		fitColumns: true,
		pagination:true,
        singleSelect:true,
        pageSize:10,
	    pageNumber:1,          
	    pageList:[10,15,20],
		columns : [[
					{field : 'createName',title : '提交人',width : $(this).width() * 0.10,align : 'center'},
					{field : 'content',title : '内容',width : $(this).width() * 0.35,align : 'center',
						formatter:function(value,rec){
							var count = '<a onclick="intoPublishDetailPage('+rec.id+');" href="javascript:void(0)">';
							if(rec.detailVoList != null && rec.detailVoList.length>0){
								$.each(rec.detailVoList,function(index,detail){
									count +=detail.infoTitle+'<br/>';
								});
							}
							count +='</a>';
							return count;
						 }
					},
					{field : 'applierName',title : '审核人',width : $(this).width() * 0.10,align : 'center'},
					{field : 'remark',title : '审核意见',width : $(this).width() * 0.25,align : 'center'},
					{field : 'applyTime',title : '审核时间',width : $(this).width() * 0.10,align : 'center',
						formatter : function(value, rec, index) {
							var str = "";
							if(value != '') {
								str = backendCommon.dateTimeToLocalStringWithSeconds(value);
							}
							return str;
						}
					},
					{field : 'updateTime',title : '更新时间',width : $(this).width() * 0.10,align : 'center',
						formatter : function(value, rec, index) {
							var str = "";
							if(value != '') {
								str = backendCommon.dateTimeToLocalStringWithSeconds(value);
							}
							return str;
						}
					},
					{field : 'opt',title : '操作',width : $(this).width() * 0.10,align : 'center',
						formatter : function(value, rec, index) {
							return '<a href="javascript:;" onclick="editInfoPushApp('+rec.id+',&quot;'+rec.infoIds+'&quot;)">编辑</a>';
						}
					}
				]]
	});
}

function initTable3(){
	$('#tt3').datagrid({
		title:'推送失败列表',
		width : backendCommon.getWidth(0.94),
		height : backendCommon.getHeight(0.80),
		method:'post',
		loadMsg:"数据装载中,请稍等....",
		url:mainDomain+'/info/findInfoPushPgByDto.action?pushStatis=9',
		rownumbers:true,
		nowrap: false,
		fitColumns: true,
		pagination:true,
        singleSelect:false,
        pageSize:10,
	    pageNumber:1,          
	    pageList:[10,15,20],
		columns : [[
		            {field:'id',checkbox:true},    
					{field : 'createName',title : '提交人',width : $(this).width() * 0.10,align : 'center'},
					{field : 'content',title : '内容',width : $(this).width() * 0.35,align : 'center',
						formatter:function(value,rec){
							var count = '<a onclick="intoPublishDetailPage('+rec.id+');" href="javascript:void(0)">';
							if(rec.detailVoList != null && rec.detailVoList.length>0){
								$.each(rec.detailVoList,function(index,detail){
									count +=detail.infoTitle+'<br/>';
								});
							}
							count +='</a>';
							return count;
						 }
					},
					{field : 'appointmentTime',title : '推送时间',width : $(this).width() * 0.10,align : 'center',
						formatter : function(value, rec, index) {
							var str = "";
							if(value != '') {
								str = backendCommon.dateTimeToLocalStringWithSeconds(value);
							}
							return str;
						}
					},
					{field : 'opt',title : '操作',width : $(this).width() * 0.10,align : 'center',
						formatter : function(value, rec, index) {
							return '<a href="javascript:;" onclick="toPushAgain('+rec.id+')">再次推送</a>';
						}
					}
				]]
	});
	
	$('#tt3').datagrid("getPager").pagination({
		buttons: [{
		  	  id:"pushArticlebtn",
		  	  text:"推送",
		  	  iconCls:'icon-undo',
		  	  handler:function(){
		  		  batchPushArticle();
		  	  },
		}]
	});
}

function editInfoPushApp(id,infoIds){
	backendCommon.openInTabs(mainDomain + '/info/push.action?infoIds='+infoIds+'&id='+id+'&opType=edit', "编辑推送申请");
	
//	window.location.href=mainDomain + '/info/push.action?infoIds='+infoIds+'&id='+id+'&opType=edit';
}
/**
 * 重新推送
 * @param id
 */
function toPushAgain(id){
	var infoPushAppDto = {};
	var infoIds = [];
	infoIds.push(id);
	infoPushAppDto['infoIds']=infoIds;
	infoPushAppDto['pushStatis']=1;
	var params = {'infoPushAppDto':JSON.stringify(infoPushAppDto)};
	$.post(mainDomain +'/info/updateInfoPushStatus.action' , params , function(data){
		if(data.success){
			$.messager.alert('提示','更新成功！','info');
			$('#tt3').datagrid('reload');
		}else{
			$.messager.alert('提示','更新异常，稍后再试！','error');
			return false;
		}
	});
}

function batchPushArticle(){
	var checkrows = $('#tt3').datagrid('getChecked');
	if(checkrows.length == 0){
		$.messager.alert('提示','请至少选择一个！','error');
		return false;
	}
	var infoIds = [];
	$.each(checkrows,function(index,item){
		infoIds.push(item.id);
	});
	var infoPushAppDto = {};
	infoPushAppDto['infoIds']=infoIds;
	infoPushAppDto['pushStatis']=1;
	var params = {'infoPushAppDto':JSON.stringify(infoPushAppDto)};
	$.post(mainDomain +'/info/updateInfoPushStatus.action' , params , function(data){
		if(data.success){
			$.messager.alert('提示','更新成功！','info');
			$('#tt3').datagrid('reload');
		}else{
			$.messager.alert('提示','更新异常，稍后再试！','error');
			return false;
		}
	});
}